package com.bicon.botu.finance.payment.alipay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayFundTransToaccountTransferModel;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.finance.payment.common.PaymentConstants;
import com.bicon.botu.finance.payment.entity.AliPayDto;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;

/**
 * @Author: 宋旭
 * @Date: Created in 10:27 2017/7/31/031
 */
@Service
@Log4j
public class AliPayService {

    private AliPayApiConfig init(String appid, String publicKey, String privateKey) {
        AliPayApiConfig aliPayApiConfig = AliPayApiConfig.New()
                .setAppId(appid)
                .setAlipayPublicKey(publicKey)
                .setCharset(PaymentConstants.CHARSET)
                .setPrivateKey(privateKey)
                .setServiceUrl(PaymentConstants.ALIPAY_GATEWAY)
                .setSignType(PaymentConstants.ALIPAY_RSA2)
                .build();
        return aliPayApiConfig;
    }

    public static void main(String[] args) {
        AliPayDto aliPayDto = new AliPayDto();
        aliPayDto.setAlipayAccount("botu@biconjs.com");
        aliPayDto.setAlipayAppId("2017071107714840");
        aliPayDto.setAlipayAppPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCKazs6MfyoxyMZ7H7EEoli5+4ZUsBTw9CRI7eEr7+q/jEwmVqCFk1bufMmc0WJ584sAb16KgQAHIszI0TyUsBgYa7iLBI19/I+BLR4Mb82ny8wjUeHS+/Aq/Mpf7/w6SUrCZ7yZiRpr5pfmPEoJYwSoVjh9fCUMGfcj5T7txWP2xHPf/b4p10WuOpcaBIMD3Irl1UCKZoyFaoKT4CSyHQ8bGp/MoxXfdSVmDDjZJ6jRO9QwAZFJ6fwas11H6/+ifAiCW3EkHhET0kKJdEAYOyc03obY2wLKmnSxFdDsAGHYqS0aSnTH6ZdYXwpMoA18ctPS7QF3knQrTE7UMOWt1SZAgMBAAECggEARWYB6emJ/azp4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lEB1JPaoD/kIbAdafNO0fakKC7XmBblowI+WrpcZI09toLQqNsFGAy2y8/2y3/qfUE8VZjP3JdKg9Y2mgdfDLgC3JkmiDqRubSY8PZbs3CLIg3zl1fZYklmxzywY7n7D/68OY=");
        aliPayDto.setAlipayAppPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAims7OjH8qMcjGex+xBKJYufuGVLAU8PQkSO3hK+/qv4xMJlaghZNW7nzJnNFiefOLAG9eioEAByLMyNE8lLAYGGu4iwSNffyPgS0eDG/Np8vMI1Hh0vvwKvzKX+/8OklKwme8mYkaa+aX5jxKCWMEqFY4fXwlDBn3I+U+7cVj9sRz3/2+KddFrjqXGgSDA9yK5dVAimaMhWqCk+Aksh0PGxqfzKMV33UlZgw42Seo0TvUMAGRSen8GrNdR+v/onwIgltxJB4RE9JCiXRAGDsnNN6G2NsCypp0sRXQ7ABh2KktGkp0x+mXWF8KTKANfHLT0u0Bd5J0K0xO1DDlrdUmQIDAQAB");
        aliPayDto.setAlipayPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlvRyNu9fTIyeV9zh2NUh07CvNQgGLUkm66mDac+h/S062fBFsU+Tqxk0/CWdMvKy3EEpUHcNGrWSl0bOJn1uuw98xqgKwZYCnk/7fEo44IVkbYQJ8Lqr4v3tTCXoxSav/3CWNKFyYoWkNSn1mq4lEpv3QvDS7hqXH3kdE15qnY/sTHpPA3UxArXB8QUamPeTg3QmlrsvG+c28Bw2aKwcM3OJRptAJXlB2ZhwQHpf5/TyWEFhtJSiwgAziEWWsGdBDqfnfDo2hwcC+OmlrPQRfjVQ5DUF1CWaDzju7Gz9i57Nivbqk8vs2FdbNZoLunt7pZzMQxerLMG7PSbxXPzIxQIDAQAB");
        aliPayDto.setPayerAccount("botu@biconjs.com");
        aliPayDto.setPayerName("伯图电子商务");

        AliPayApiConfig aliPayApiConfig = AliPayApiConfig.New()
                .setAppId(aliPayDto.getAlipayAppId())
                .setAlipayPublicKey(aliPayDto.getAlipayPublicKey())
                .setCharset(PaymentConstants.CHARSET)
                .setPrivateKey(aliPayDto.getAlipayAppPrivateKey())
                .setServiceUrl(PaymentConstants.ALIPAY_GATEWAY)
                .setSignType(PaymentConstants.ALIPAY_RSA2)
                .build();
        System.out.println(123);

    }

    /**
     * app支付
     * @param code
     *         支付应用code
     * @param appid
     *         应用的appId
     * @param publicKey
     *         应用的公钥
     * @param privateKey
     *         应用的私钥
     * @param orderNum
     *         支付版本号
     * @param subjectName
     *         订单名称
     * @param body
     *         订单内容
     * @param money
     *         订单金额
     * @return
     */
    public String appPay(String code, String appid, String publicKey, String privateKey, String orderNum,
                         String subjectName, String body, String money) {
        try {
            AliPayApiConfig aliPayApiConfig = init(appid, publicKey, privateKey);
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.setBody(body);
            model.setSubject(subjectName);
            model.setOutTradeNo(orderNum);
            model.setTimeoutExpress("30m");
            model.setTotalAmount(money);
            model.setPassbackParams(code);
            model.setProductCode("QUICK_MSECURITY_PAY");
            String orderInfo = AliPayApi.startAppPayStr(aliPayApiConfig, model, PaymentConstants.ALIPAY_PAY_NOTIFY);
            return orderInfo;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 单笔转账到支付宝账户
     * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.54Ty29&
     * treeId=193&articleId=106236&docType=1
     * @param name
     *         付款机构名称,APP名称
     * @param appid
     *         应用的appId
     * @param publicKey
     *         应用的公钥
     * @param privateKey
     *         应用的私钥
     * @param orderNum
     *         订单号
     * @param payeeAccount
     *         收款账号
     * @param amount
     *         金额
     * @param payeeName
     *         收款人姓名
     * @param remark
     *         备注信息
     * @return
     */
    public boolean transfer(String name, String appid, String publicKey, String privateKey, String orderNum, String payeeAccount,
                            String amount, String payeeName, String remark) {
        AliPayApiConfig aliPayApiConfig = init(appid, publicKey, privateKey);
        boolean isSuccess = false;
        AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();
        model.setOutBizNo(orderNum);
        model.setPayeeType("ALIPAY_LOGONID");
        model.setPayeeAccount(payeeAccount);
        model.setAmount(amount);
        model.setPayerShowName(name);
        model.setPayerRealName(payeeName);
        model.setRemark(remark);
        try {
            isSuccess = AliPayApi.transfer(aliPayApiConfig, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    /**
     * 退款交易
     * @param appid         应用的appId
     * @param publicKey     应用的公钥
     * @param privateKey    应用的私钥
     * @param orderNum      订单号
     * @param refundMoney   退款金额
     * @param refundNo      退款编号
     * @param refundAccount 退款操作人账户名
     * @param refundReason  退款原因
     * @param storeId       商户id
     * @return
     */
    public String tradeRefund(String appid, String publicKey, String privateKey, String orderNum, String refundMoney, String refundNo, String refundAccount, String refundReason, String storeId) {
        try {
            AliPayApiConfig aliPayApiConfig = init(appid, publicKey, privateKey);
            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
            model.setOutTradeNo(orderNum);
            //支付宝交易号
            model.setTradeNo("");
            //"退款金额"
            model.setRefundAmount(refundMoney);
            //商户操作员Id
            model.setOperatorId(refundAccount);
            model.setOutRequestNo(refundNo);
            //退款说明
            model.setRefundReason(refundReason);
            //商户的门店Id
            model.setStoreId(storeId);
            String tradeRefundResponse = AliPayApi.tradeRefund(aliPayApiConfig, model);
            return tradeRefundResponse;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*public static void main(String[] args) {
        //获取app在支付宝平台的相关信息
        AliPayDto aliPayDto = new AliPayDto();
        aliPayDto.setAlipayAccount("botu@biconjs.com");
        aliPayDto.setAlipayAppId("2017071107714840");
        aliPayDto.setAlipayAppPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCKazs6MfyoxyMZ7H7EEoli5+4ZUsBTw9CRI7eEr7+q/jEwmVqCFk1bufMmc0WJ584sAb16KgQAHIszI0TyUsBg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Y/sTHpPA3UxArXB8QUamPeTg3QmlrsvG+c28Bw2aKwcM3OJRptAJXlB2ZhwQHpf5/TyWEFhtJSiwgAziEWWsGdBDqfnfDo2hwcC+OmlrPQRfjVQ5DUF1CWaDzju7Gz9i57Nivbqk8vs2FdbNZoLunt7pZzMQxerLMG7PSbxXPzIxQIDAQAB");
        aliPayDto.setPayerAccount("botu@biconjs.com");
        aliPayDto.setPayerName("伯图电子商务");
       tradeRefund(aliPayDto.getAlipayAppId(), aliPayDto.getAlipayPublicKey(),
                aliPayDto.getAlipayAppPrivateKey(), "20180625053006815H", "0.01", "20180625", "admin", "测试退款", "");
    }*/


}
